package com.app.core.util;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * @author MaTianyu
 * @date 2014-11-21
 */
public class NumberUtil {

    public static int convertToint(String intStr, int defValue) {
        try {
            return Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return defValue;
    }

    public static long convertTolong(String longStr, long defValue) {
        try {
            return Long.parseLong(longStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return defValue;
    }

    public static float convertTofloat(String fStr, float defValue) {
        try {
            return Float.parseFloat(fStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return defValue;
    }

    public static double convertTodouble(String dStr, double defValue) {
        try {
            return Double.parseDouble(dStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return defValue;
    }

    public static Integer convertToInteger(String intStr) {
        try {
            return Integer.parseInt(intStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return null;
    }

    public static Long convertToLong(String longStr) {
        try {
            return Long.parseLong(longStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return null;
    }

    public static Float convertToFloat(String fStr) {
        try {
            return Float.parseFloat(fStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return null;
    }

    public static Double convertToDouble(String dStr) {
        try {
            return Double.parseDouble(dStr);
        } catch(NumberFormatException e) {
            // e.printStackTrace();
        }
        return null;
    }


    /**
     * setEditText 限制小数点位数
     * @param editText
     * @param decimal_digits
     */
    public static void setEditTextPoint(final EditText editText ,final int decimal_digits) {
        editText.addTextChangedListener(new TextWatcher() {


            @Override
            public void onTextChanged(CharSequence s, int start, int before,int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > decimal_digits) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + decimal_digits+1);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {

            }
            @Override
            public void afterTextChanged(Editable s) {

            }

        });
    }


}
